﻿using System;
using System.IO;
using VsxFactory.Modeling.Strategies;
using System.Collections.Generic;
using Microsoft.VisualStudio.Settings;
using Microsoft.VisualStudio.Shell.Settings;
using System.Linq;
using System.Text;
using VsxFactory.Modeling.Repository;

namespace VsxFactory.Modeling.StrategyManager
{
    /// <summary>
    /// 
    /// </summary>
    internal class VisualStudioStrategySettings : SettingsBase, IPackageSourceSettingsManager
    {
        private const string PackageSourcesSettingProperty = "PackageSources";
        private const string ActivePackageSourceSettingProperty = "ActivePackageSource";
        private const string SettingsRoot = "VsxFactory.StrategyManager";
        
        private string _baseFolder;
        private WritableSettingsStore _userSettingsStore;
        private IServiceProvider _serviceProvider;

        /// <summary>
        /// Initializes a new instance of the <see cref="SettingsService"/> class.
        /// </summary>
        /// <param name="serviceProvider">The service provider.</param>
        public VisualStudioStrategySettings(IServiceProvider serviceProvider)
        {
            System.Diagnostics.Contracts.Contract.Requires(serviceProvider != null);

            _serviceProvider = serviceProvider;
            _baseFolder = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), VsxFactoryConstants.Name);
            System.Diagnostics.Contracts.Contract.Assert(_baseFolder != null);
        }

        private string ResolveFolder(string name)
        {
            System.Diagnostics.Contracts.Contract.Requires(!String.IsNullOrWhiteSpace(name));
            string path = Path.Combine(_baseFolder, name);
            Directory.CreateDirectory(path);
            return path;
        }

        public override string Folder
        {
            get { return _baseFolder; }
        }

        private WritableSettingsStore UserSettingsStore
        {
            get
            {
                if (_userSettingsStore == null)
                {
                    SettingsManager settingsManager = new ShellSettingsManager(_serviceProvider);
                    _userSettingsStore = settingsManager.GetWritableSettingsStore(SettingsScope.UserSettings);

                    // Ensure that the package collection exists before any callers attempt to use it.
                    if (!_userSettingsStore.CollectionExists(SettingsRoot))
                    {
                        _userSettingsStore.CreateCollection(SettingsRoot);
                    }
                }
                return _userSettingsStore;
            }
        }

        public string ActivePackageSourceString
        {
            get
            {
                return UserSettingsStore.GetString(SettingsRoot, ActivePackageSourceSettingProperty, null);
            }
            set
            {
                _userSettingsStore.SetString(SettingsRoot, ActivePackageSourceSettingProperty, value ?? String.Empty);
            }
        }

        public bool IsFirstRunning
        {
            get
            {
                return PackageSourcesString != null;
            }
            set
            {               
            }
        }

        public string PackageSourcesString
        {
            get
            {
                return UserSettingsStore.GetString(SettingsRoot, PackageSourcesSettingProperty, null);
            }
            set
            {
                UserSettingsStore.SetString(SettingsRoot, PackageSourcesSettingProperty, value ?? String.Empty);
            }
        }

        public override PackageSource DefaultRepositorySource
        {
            get
            {
                return new PackageSource("Default", "http://VisualStudioRepository/strategiesGallery");
            }
        }
    }
}
